package inventory.config.web;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author RenHaiquan
 * @create 2019-11-19 17:34
 */

//@WebFilter(displayName = "CORFilter",urlPatterns = "/*")
public class CORFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        System.out.println("进入跨域设置");
        /*因跨域后session不一致，添加代码 begin ----*/
        HttpServletRequest request2 = (HttpServletRequest) servletRequest;
        HttpServletResponse response2 = (HttpServletResponse) servletResponse;
        response2.setHeader("Access-Control-Allow-Origin", request2.getHeader("Origin"));
        response2.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response2.setHeader("Access-Control-Max-Age", "3600");
        response2.setHeader("Access-Control-Allow-Headers", "x-requested-with");
        response2.setHeader("Access-Control-Allow-Credentials","true"); //是否支持cookie跨域
        filterChain.doFilter(servletRequest,servletResponse);
        /* ----end----*/
    }

    @Override
    public void destroy() {

    }
}
